package service

import (
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

func AddOneActivity(Activity models.SysStuActivities) error {
	return dao.DB.Create(&Activity).Error
}

func GetActivityList(queryInfo request.QueryInfo) (Activitys []models.SysStuActivities, total int, err error) {
	if queryInfo == (request.QueryInfo{}){
		err = dao.DB.Where(&models.SysStuActivities{Type:queryInfo.Type}).Preload("Student").Find(&Activitys).Count(&total).Error
		return
	}
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&models.SysStuActivities{}).Where("type = ?", queryInfo.Type).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Where(&models.SysStuActivities{Type:queryInfo.Type}).Preload("Student").Find(&Activitys).Error
	} else {
		if err = dao.DB.Model(&models.SysStuActivities{}).Where("type = ? and activity_name like ?", queryInfo.Type, queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("type = ? and activity_name like ?", queryInfo.Type, queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Student").Find(&Activitys).Error
	}
	return
}

func UpdateActivity(Activity models.SysStuActivities) error {
	return dao.DB.Save(&Activity).Error
}

func DeleteActivity(id int) error {
	return dao.DB.Where("id = ?", id).Delete(&models.SysStuActivities{}).Error
}


